package com.ljm.controller;/**
 * @author： li
 * @date： 2022-05-13 15:28
 * @version 1.0
 */

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author： ljm
 * @date： 2022-05-13 15:28
 * @version 1.0
 */
@WebServlet("/readFile")
public class readFileServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        res.setContentType("text/json;charset=utf-8");
        PrintWriter out =res.getWriter();
        String target = req.getServletContext().getRealPath("")+"upload/";
        System.out.println(target);
        File f =new File(target);
        JSONArray ja = new JSONArray();
        JSONObject jo =new JSONObject();
        if(!f.exists()){
            out.println("没有这个文件");
            return;
        }
        File[] fa =f.listFiles();
        for(int i=0;i<fa.length;i++){
            File fs =fa[i];
            System.out.println(req.getServletContext().getContextPath());
            jo.put("file","https://www.ljmspace.cloud"+req.getServletContext().getContextPath()+"/upload/"+fs.getName());
            ja.add(jo);
        }
        out.println(ja);
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
